<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 汇金微信
 */
class HuijinweixinController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $domin = "www.wanglianfu.com";

        $return = $this->orderadd( $parameter );
        $data = [];

        $data['account'] = $this->config['mch_id'];
        $data['money'] = number_format($return['amount'],2,'.','');
        $data['order_no'] = $return['orderid'];
        //$data['token'] = $this->config['signkey'];
        switch($this->config['paytype']) {
            case 1:
                $data['pay_way'] = '1';
                break;
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                $data['pay_way'] = '0';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //"account=test&money=100.00&order_no=123&token=aaadfd&pay_way=2xxx.com"
        $str = "account={$data['account']}&money={$data['money']}&order_no={$data['order_no']}&pay_way={$data['pay_way']}{$domin}{$this->config['signkey']}";

        //echo $str;exit;
        $data['sign'] = md5($str);

        //echo "<pre/>"; print_r($data);
        //echo "签名字符串：" . $str.'<br/>'; exit;

        po_get($this->config['gateway'], $data);
    }

    public function notifyurl(){
        //$this->debug_callback();
        if(!$this->checkip(getIP(),['47.90.4.34'])){
            //exit("wrong");
            exit($this->message("成功"));
        }

        $p = I("post.");

        //$p = array ( 'token' => '0a9fdcb6e31f5d9cb9d38d94f83c9654', 'order_no' => '2019060816320310268', 'trade_no' => '201906081631598711764', 'result' => '0', 'sign' => '218db62fc2f28b27954c391f64d5b8d6', );

        //获取订单信息
        $orderid = $p['order_no'];
        $find = $this->checkOrderNotify($orderid);

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['trade_no'])
        ));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $str = $this->config['signkey'] . md5($this->config['mch_id']);
        $token2 = md5($str);

        if($p['token'] !== $token2){
            exit($this->message("成功"));
        }

        $sign = $p['sign'];
        $str = "token={$p['token']}&order_no={$p['order_no']}&trade_no={$p['trade_no']}&result={$p['result']}".$this->config['mch_id'];

        $sign2 = md5($str);

        if($sign !== $sign2){
            exit($this->message("成功"));
        }

        if($p['result'] !== '0'){
            exit($this->message("成功"));
        }

        //处理支付
        $this->async($find['pay_orderid'], $this->message("成功"));
    }

    private function message($msg){
        return json_encode(['code'=>0,'msg'=>$msg]);
    }
}